package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.Encrypt;
import com.example.lotterysystem.dao.dataobject.UserDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @auther falls_vc
 * description:
 * @date 2025/5/11  19:43
 */
@Mapper
public interface UserMapper {
    /**
     * 查询邮箱绑定人数
     * @param email
     * @return
     */
    @Select("select count(*) from user where email=#{email}")
    int countByMail(@Param("email") String email);

    /**
     * 查询手机号使用人数
     * @param phoneNumber
     * @return
     */
    @Select("select count(*) from user where phone_number=#{phoneNumber}")
    int countByPhone(@Param("phoneNumber") Encrypt phoneNumber);

    /**
     * 插入用户信息
     * @param userDO
     */
    @Insert("insert into user(user_name,email,identity,password,phone_number) " +
            " values(#{userName},#{email},#{identity},#{password},#{phoneNumber})")
    @Options(useGeneratedKeys = true, keyProperty = "id",keyColumn = "id")
    void insert(UserDO userDO);

    /**
     * 通过手机号查用户信息
     * @param phoneNumber
     * @return
     */
    @Select("select * from user where phone_number=#{phoneNumber}")
    UserDO selectByPhoneNumber(Encrypt phoneNumber);

    /**
     * 通过邮箱号查用户信息
     * @param email
     * @return
     */
    @Select("select * from user where email=#{email}")
    UserDO selectByMail(String email);

    /**
     * 通过身份信息查询用户
     * 身份信息为null时查询所有用户
     * @param identity
     * @return
     */
    @Select("<script> select * from user " +
            " <if test=\"identity != null\">" +
            " where identity=#{identity}" +
            " </if>" +
            " order by id desc" +
            "  </script>")
    List<UserDO> selectUserListByIdentity(String identity);

    /**
     * 创建活动通过 用户ids  查询ids 是否存在
     * @param ids
     * @return
     */
    @Select("<script> select id from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')' > " +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<Long> selectExistIdsByIds(@Param("items") List<Long> ids);

    @Select("<script> select * from user" +
            " where id in" +
            " <foreach item='item' collection='items' open='(' separator=',' close=')' > " +
            " #{item}" +
            " </foreach>" +
            " </script>")
    List<UserDO> batchSelectByIds(@Param("items") List<Long> ids);
}
